#!/usr/bin/env python
#
#       page-archive.py
#
#
#       Copyright (C) 2011 Joe Tannenbaum <jtannen1@binghamton.edu>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#

import os, sys
from mainwindow import MainWindow
from archive import Archive

config_ops = ['PROFILE','VIEWER','SCANCMD']

default_config="""# This is the configuration file for page-archive
PROFILE=default

# The command with which to view images
VIEWER=gthumb --viewer

# The command with which to scan images
SCANCMD=scanimage --format=TIFF --resolution 200"""

def main():
    config = {'PROFILE' : 'default',
             'VIEWER' : 'gthumb --viewer',
             'SCANCMD' : 'scanimage --format=TIFF --resolution 200',
            }

    print 'Reading config file...'
    try:
        config_file = open('config')
        for line in config_file.read().split('\n'):
            if not line.startswith('#') and line.strip() and '=' in line:
                config[line.split('=')[0].upper()] = '='.join(line.split('=')[1:])
        config_file.close()
    except IOError:
        print 'File not found! Creating default...'
        config_file = open('config','w')
        config_file.write(default_config)
        config_file.close()
        try: os.mkdir('default')
        except: pass
        try: dbfile = open('default/db')
        except IOError: dbfile = open('default/db','w') # UGH
        dbfile.close()
    for k in config.keys():
        print '%s=%s' % (k, config[k])

    print 'Creating working archive and GUI...'
    archive = Archive(config)
    newwindow = MainWindow(archive)
    return 0

if __name__ == '__main__': main()
